Public Class ProfitabilityReport
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents litReport As System.Web.UI.WebControls.Literal
    Protected WithEvents lblCustomerName As System.Web.UI.WebControls.Label
    Protected WithEvents ddlSupplier As System.Web.UI.WebControls.DropDownList
    Protected WithEvents litReportForCN As System.Web.UI.WebControls.Literal
    Protected WithEvents lblGrandTotalCost As System.Web.UI.WebControls.Label
    Protected WithEvents lblGrandTotalValue As System.Web.UI.WebControls.Label
    Protected WithEvents lblGrandTotalMargin As System.Web.UI.WebControls.Label
    Protected WithEvents lblGrandTotalMarginPercent As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Protected WithEvents rflc As ReportFunctionsLinkCtrl

    Public Const PROFITABILITY_REPORT_SESSION_NAME As String = "MIMS_WRS.Web.ProfitabilityReport"

#Region "   Properties"
    Protected Property PageBO() As CProfitabilityReport
        Get
            Return CType(Session(Me.PROFITABILITY_REPORT_SESSION_NAME), CProfitabilityReport)
        End Get
        Set(ByVal Value As CProfitabilityReport)
            Session(Me.PROFITABILITY_REPORT_SESSION_NAME) = Value
        End Set
    End Property

    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim cid As Integer
            Try
                cid = CInt(Request.QueryString("cid"))
            Catch ex As Exception
                cid = -1
            End Try

            If Not cid > 0 Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return cid
        End Get
    End Property
#End Region

    Private Sub BindGrid()
        PageBO.Output = Report.EReportOutputType.Screen
        PageBO.CustomerID = Me.CustomerID
        PageBO.SupplierID = CInt(Me.ddlSupplier.SelectedItem.Value)
        PageBO.RefreshReport()
        Me.litReport.Text = PageBO.GetReportHTML
        Me.litReportForCN.Text = PageBO.GetReportHTMLForCN
        Me.lblGrandTotalCost.Text = FormatNumber(PageBO.GrandTotalCost, 2)
        Me.lblGrandTotalValue.Text = FormatNumber(PageBO.GrandTotalValue, 2)

        Me.lblGrandTotalValue.ForeColor = System.Drawing.Color.DarkBlue

        ' If Value is negative, show in red
        If PageBO.GrandTotalValue < 0 Then
            Me.lblGrandTotalValue.ForeColor = System.Drawing.Color.Red
            Me.lblGrandTotalMargin.ForeColor = System.Drawing.Color.Red
            Me.lblGrandTotalMarginPercent.ForeColor = System.Drawing.Color.Red
        End If

        Me.lblGrandTotalMargin.Text = FormatNumber(PageBO.GrandTotalMargin, 2)
        Try
            Me.lblGrandTotalMarginPercent.Text = FormatNumber(PageBO.GrandTotalMarginPercent, 2)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()

            Header1.MenuIndex = EMenuIndex.InternalReport

            Me.Header1.NavigationBar.Add("Back to Customer Reports List", AppRoot() & "/Internal/Reports.aspx")

            Me.lblCustomerName.Text = CUser.GetName(Me.CustomerID)

            Dim dt As DataTable = COrder.GetSupplierListFromOrders(Me.CustomerID)
            dt.DefaultView.Sort = "SUPPLIER_CODE"

            Me.ddlSupplier.DataSource = dt.DefaultView
            Me.ddlSupplier.DataTextField = "SUPPLIER_CODE"
            Me.ddlSupplier.DataValueField = "LKD_ID_PK"
            Me.ddlSupplier.DataBind()
            Me.ddlSupplier.Items.Insert(0, New ListItem("[All]", "0"))

            Dim objReport As New CProfitabilityReport
            Me.PageBO = objReport

            Me.BindGrid()
        End If
    End Sub

    Private Sub rflc_ExportClick() Handles rflc.ExportClick
        PageBO.Output = Report.EReportOutputType.File
        'output to user
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""" & "PR" & GetRandomFileName() & ".xls""")
        Response.Write(PageBO.GetReportHTML)

        Response.Write("<TABLE cellSpacing=0 cellPadding=0 width=""100%""  border=0><tr>")
        Response.Write("<td class='txtnormal'>")
        Response.Write("</td>")
        Response.Write("</tr>")

        Response.Write("<tr>")
        Response.Write("<td class='txtnormal'><strong>Credit Notes Report </strong>")
        Response.Write("</td>")
        Response.Write("</tr></table>")

        Response.Write(PageBO.GetReportHTMLForCN)

        Response.Write("<TABLE cellSpacing=0 cellPadding=0 width=""100%""  border=0>")
        Response.Write("<tr>")
        Response.Write("<td class='txtnormal'>")
        Response.Write("</td>")
        Response.Write("</tr>")

        Response.Write("<tr>")
        Response.Write("<td class='txtnormal'><strong>Grand Total </strong>")
        Response.Write("</td>")
        Response.Write("</tr></table>")

        Response.Write("<TABLE cellSpacing=1 cellPadding=3 width=""100%""  border=1>" & _
                "<TBODY>" & _
                 "<TR class=""txtheader"">" & _
                  "<TD align=""center"" colspan=""4"">&nbsp;</TD>" & _
                  "<TD align=""center"" width=""100""><strong>Cost<br>" & _
                  "		<em>(USD)</em></strong></TD>" & _
                  "<TD align=""center"" width=""100""><strong>Value<br>" & _
                  "		<em>(USD)</em></strong></TD>" & _
                  "<TD align=""center"" width=""101""><strong>Margin $ <em>(USD)</em></strong></TD>" & _
                  "<TD align=""center"" width=""100""><strong>Margin % <em></em></strong>" & _
                  "</TD>" & _
                 "</TR>")

        Dim marginPercent As String

        Try
            marginPercent = FormatNumber(PageBO.GrandTotalMarginPercent, 2)
        Catch ex As Exception
            marginPercent = "N/A"
        End Try

        Dim fontRed As String

        If PageBO.GrandTotalValue < 0 Then
            fontRed = "color='Red'"
        Else
            fontRed = ""
        End If

        Response.Write("<TR class=""bgrow1"">" & _
                  "<TD align=""center"" colspan=""4"">&nbsp;</TD>" & _
                  "<TD align=""center"" width=""100""><strong>" & FormatNumber(PageBO.GrandTotalCost, 2) & "</strong></TD>" & _
                  "<TD align=""center"" width=""100""><strong><font " & fontRed & ">" & FormatNumber(PageBO.GrandTotalValue, 2) & "</font></strong></TD>" & _
                  "<TD align=""center"" width=""101""><strong><font " & fontRed & ">" & FormatNumber(PageBO.GrandTotalMargin, 2) & "</font></strong></TD>" & _
                  "<TD align=""center"" width=""100""><strong><font " & fontRed & ">" & marginPercent & "</font></strong>" & _
                  "</TD>" & _
                 "</TR>")


        Response.Write("</tbody></table>")

        Response.End()
    End Sub

    Private Sub ddlSupplier_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSupplier.SelectedIndexChanged
        Me.BindGrid()
    End Sub
End Class
